<?php
defined('APP_PATH') or die ('Unauthorized access'); 

/**
 * widget
 * @author Chris Zhou
 *
 */
class WidgetController extends Controller
{
	private $_blog = null;
	private $_category = null;
	public function __construct()
	{
		$this->_blog = Load::Model('blog');
		$this->_category = Load::Model('category');
	}
	
	public function category()
	{
		$category = $this->_category->getCategory();
		
		if(!empty($category) && count($category) > 0){
			foreach($category as $k => $v){
				$category[$k]['counts'] = $this->_blog->getBlogCount($v['id']);
			}
		}
		
		if(!empty($category) && count($category) > 0){
			echo '<ul>';
			foreach($category as $list){
				echo "<li><a href='/?c=list&id=".$list['id']."'>{$list['category']} ({$list['counts']})</a></li>";
			}
			echo '</ul>';
		}
		
	}
	
	public function newblog()
	{
		$bloglist = $this->_blog->getBlogList(0,1,10);
		if(!empty($bloglist) && count($bloglist) > 0){
			echo '<ul>';
			foreach($bloglist as $list){
				echo "<li><a href='/?c=detail&id={$list['id']}'>{$list['title']}</a></li>";
			}
			echo '</ul>';
		}
	}
	
	public function randblog()
	{
		$bloglist = $this->_blog->getBlogList(0,1,200);
		if(!empty($bloglist) && count($bloglist) > 0){
			shuffle($bloglist);
			$bloglist = array_slice($bloglist,0,10);
			
			if(!empty($bloglist) && count($bloglist) > 0){
				echo '<ul>';
				foreach($bloglist as $list){
					echo "<li><a href='/?c=detail&id={$list['id']}'>{$list['title']}</a></li>";
				}
				echo '</ul>';
			}
		}
		
	}
	
	public function tagcloud()
	{
		$bloglist = $this->_blog->getBlogList(0,1,200);
		if(!empty($bloglist) && count($bloglist) > 0){
			$tags = array();
			foreach($bloglist as $list){
				if(!empty($list['tags'])){
					$tag = explode(' ',trim($list['tags']));
					if(!empty($tag) && count($tag) > 0){
						foreach($tag as $k => $v){
							$tags[] = $v;
						}
					}
				}
			}
			
			return array_unique($tags);
		}
	}
	
	public function newcomments()
	{
		
	}
}